package com.joplus.housekeeper.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hyphenate.util.DensityUtil;
import com.joplus.housekeeper.bean.HomePageBean;
import com.joplus.housekeeper.utils.DensityUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zisheng on 2017/7/20.
 */

public class BarHorizontalAdapter implements View.OnClickListener {
    LinearLayout BarLin;
    Context context;
    List<HomePageBean.DataBean.HomepageListBean> bean;
    private LayoutInflater layout;
    private int width;
    private int heigh;
    private int margin;


    public BarHorizontalAdapter(Context context, LinearLayout ll, List<HomePageBean.DataBean.HomepageListBean> bean) {
        this.context = context;
        this.BarLin = ll;
        this.bean = bean;
        layout = LayoutInflater.from(context);
        width = DensityUtil.dip2px(context, 82);
        heigh = DensityUtil.dip2px(context, 68);
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int w = windowManager.getDefaultDisplay().getWidth();
        if (bean.size() == 4) {
            margin = (w - width * 4) / 8;
        } else {
            margin = (w - width * 4 - DensityUtils.dip2px(context, 5)) / 9;
        }

        addView();

    }

    List<ImageView> images = new ArrayList<>();

    private void addView() {
        BarLin.removeAllViews();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < bean.size(); i++) {
                    ImageView imageView = new ImageView(context);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, heigh);
                    layoutParams.setMargins(margin, 0, margin, 0);
                    imageView.setLayoutParams(layoutParams);
                    imageView.setOnClickListener(BarHorizontalAdapter.this);
                    imageView.setTag(i);
                    ImageLoader.getInstance().displayImage(bean.get(i).getIconKey(), imageView);
                    images.add(imageView);

                }
                BarLin.post(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < images.size(); i++) {
                            BarLin.addView(images.get(i));

                        }
                    }
                });

            }
        }).start();

        //    BarLin.requestLayout();
    }


    @Override
    public void onClick(View view) {
        Object tag = view.getTag();
        if (itemClick != null) {
            itemClick.click((Integer) tag);
        }

    }

    ItemClick itemClick;

    public void onItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }

    public interface ItemClick {
        void click(Integer s);
    }
}
